package com.Akronmcp.hb;

import java.util.ArrayList;
import java.util.List;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import com.Akronmcp.hb.HbLoader.ReadInfo;
import com.Akronmcp.hta.main.HLoader;
import com.Akronmcp.hta.obj.HManager;
import com.Akronmcp.hta.obj.HopBook;
import com.Akronmcp.hta.obj.HopChat;
import com.Akxymcp.hta.command.ComPath;
import com.Akxymcp.hta.command.ValuePath;


/**指令处理类*/
public class HbComer implements CommandExecutor,TabCompleter
{

	static HManager hta = HbM.hta;
	static FileConfiguration lang = HbM.lang;
	

	//各种子指令
	
	@Override
    public boolean onCommand(CommandSender sd, Command cmd, String label, String[] args)
    {
       return hb.input(sd, args);
    }	
	
	static ComPath hb;
	static
	{
		hb = new ComPath("hb");
		
		hb.setComfun((sd,v)->
		{
			if(sd instanceof Player)
			{
				Player p = (Player)sd;
				if(p.hasPermission("hb.admin"))
					hta.sendTellraw("bhelp1",p);
				else
					hta.sendTellraw("buhelp1",p);
			}else
			{
				sd.sendMessage(help("help"));
			}
			return true;
		}).setPerm("hb.users");
		
		hb.addPath(new ComPath("reload")
			.setComfun((sd,v)->{
				try {
					HbLoader.reload_conf();
					sd.sendMessage(help("loadok"));
				}catch (Exception e) {
					e.printStackTrace();
					sd.sendMessage(help("loadnotok"));
				}
				return true;
			}).setPerm("hb.admin"));
		
		hb.addPath(new ComPath("open")
			.addValuePath((ValuePath) new ValuePath("[bookid]")
				.setSourceGetor((sd)->{
					return new ArrayList<String>(HManager.hbookMap.keySet());
				})
				.setComfun((sd,v)->{
					Player p = (Player)sd;
					ReadInfo ri = HbLoader.RI.get(p.getUniqueId());
					HopBook thb = hta.getHopBook(v.get(0));
					if(thb==null)
					{
						p.sendMessage(help("nobook"));
						return true;
					}
					
					if(ri==null)
					{
						HbLoader.RI.put(p.getUniqueId(),new ReadInfo(thb)
						{{
							HopChat htx = view();
							if(htx!=null)
								see(p, htx);
							else
								p.sendMessage(help("nopage"));
						}});
					}else
					{
						if(thb.equals(ri.curbook))
						{
							HopChat htx = ri.view();
							if(htx!=null)
								ri.see(p, htx);
							else
								p.sendMessage(help("nopage"));
						}else
						{
							HopChat htx = ri.turnTo(thb,0);
							if(htx!=null)
								ri.see(p, htx);
							else
								p.sendMessage(help("nopage"));
						}
					}
					return true;
				}).setInGame(true)));
		
		hb.addPath(new ComPath("home")
			.setComfun((sd,v)->{
				Player p = (Player)sd;
				ReadInfo ri = HbLoader.RI.get(p.getUniqueId());
				if(ri==null)
				{
					p.sendMessage(help("notopened"));
				}else
				{
					HopChat htx = ri.view(0);
					if(htx!=null)
						ri.see(p, htx);
					else
						p.sendMessage(help("nopage"));
				}
				return true;
			}).setInGame(true));
		
		hb.addPath(new ComPath("back")
			.setComfun((sd,v)->{
				Player p = (Player)sd;
				ReadInfo ri = HbLoader.RI.get(p.getUniqueId());
				if(ri==null)
				{
					p.sendMessage(help("notopened"));
				}else
				{
					HopChat htx = ri.back();
					if(htx!=null)
						ri.see(p, htx);
					else
					{
						htx = ri.view();
						if(htx!=null)
							ri.see(p, htx);
						else
							p.sendMessage(help("nopage"));
					}
				}
				return true;
			}).setInGame(true));
		
		hb.addPath(new ComPath("next")
			.setComfun((sd,v)->{
				Player p = (Player)sd;
				ReadInfo ri = HbLoader.RI.get(p.getUniqueId());
				if(ri==null)
				{
					p.sendMessage(help("notopened"));
				}else
				{
					HopChat htx = ri.next();
					if(htx!=null)
						ri.see(p, htx);
					else
					{
						htx = ri.view();
						if(htx!=null)
							ri.see(p, htx);
						else
							p.sendMessage(help("nopage"));
					}
				}
				return true;
			}).setInGame(true));
		
		hb.addPath(new ComPath("to")
			.addValuePath((ValuePath) new ValuePath("[page]")
				.setSourceGetor((sd)->
				{
					Player p = (Player)sd;
					ReadInfo ri = HbLoader.RI.get(p.getUniqueId());
					if(ri==null)
					{
						return new ArrayList<>();
					}else
					{
						return new ArrayList<>(ri.curbook.getPages().keySet());
					}
				})
				.setComfun((sd,v)->{
					Player p = (Player)sd;
					ReadInfo ri = HbLoader.RI.get(p.getUniqueId());
					
					if(ri==null)
					{
						p.sendMessage(help("notopened"));
					}else
					{
						String page = v.get(0);
						if(page.equals("+"))
						{
							HopChat htx = ri.view(ri.index+1);
							if(htx!=null)
								ri.see(p, htx);
							else
							{
								htx = ri.view();
								if(htx!=null)
									ri.see(p, htx);
								else
									p.sendMessage(help("nopage"));
							}
							
							return true;
						}else if(page.equals("-"))
						{
							HopChat htx = ri.view(ri.index-1);
							if(htx!=null)
								ri.see(p, htx);
							else
							{
								htx = ri.view();
								if(htx!=null)
									ri.see(p, htx);
								else
									p.sendMessage(help("nopage"));
							}
							return true;
						}
						
						try {
							int pi = Integer.valueOf(page);
							HopChat htx = ri.view(pi-1);
							if(htx!=null)
								ri.see(p, htx);
							else
								p.sendMessage(help("nopage"));
						} catch (Exception e) {
							HopChat htx = ri.view(page);
							if(htx!=null)
								ri.see(p, htx);
							else
								p.sendMessage(help("nopage"));
						}
					}
					return true;
				}).setInGame(true)));
		
		
	}
	
	
	
	public static String help(String path)
	{
		String str = null;
		lang = HbM.lang; 
		if(!path.contains("help"))
		{
			str = lang.getString("prefix","");
		}
		
		str+= lang.getString(path,path);
		str = str.replace("&","§");
		return str;
	}
	
	/**指令补全*/
	@Override
    public List<String> onTabComplete(CommandSender sd, Command cmd, String alias, String[] args) 
    {//tab补全处理
    	return hb.tabInput(sd, args);
    }
     

	
}
